<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{$site.name|htmlentities}</title>
    <link rel="stylesheet" href="static/css/admin.css">
    <link rel="stylesheet" href="static/css/layui.css">
    <style>
        .usage-instructions {
            background-color: #f6f8fa;
            border-left: 5px solid #1E9FFF;
            padding: 20px;
            margin-bottom: 20px;
        }
        .usage-instructions h4 {
            font-size: 18px;
            margin-bottom: 10px;
        }
        .usage-instructions ul {
            list-style: none;
            padding: 0;
        }
        .usage-instructions ul li {
            margin-bottom: 10px;
            line-height: 1.6;
        }
        .layui-table th {
            background-color: #f2f2f2;
        }
        .layui-table td, .layui-table th {
            text-align: center;
            vertical-align: middle;
        }
        .student-list-title {
            font-size: 22px;
            font-weight: bold;
            margin-top: 40px;
            margin-bottom: 20px;
            color: #333;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
    </style>
</head>
<body class="layui-bg-gray">
    <div class="layui-container" style="padding-top: 50px;">
    <div class="layui-row">
        <div class="layui-col-xs12 layui-col-sm8 layui-col-sm-offset2 layui-col-md6 layui-col-md-offset3">
            <div class="layui-card">
                <div class="layui-card-header layui-text-center">
                    <h3>{$site.name|htmlentities}</h3>
                </div>
                <div class="layui-card-body">
                    <div class="usage-instructions">
                        <h3>剩余卡密数量：</h3>
                        <p style="font-size: 16px; color: #333;">当前剩余：<strong>{$count}</strong> 张</p>
                    </div>
                    
                    <div class="usage-instructions">
                        <h4>使用说明：</h4>
                        <ul>
                            <li>1.一个月一次 。</li>
                            <li>2. 自觉点，验证邮箱</li>
                            <li>3. 没了</li>
                        </ul>
                    </div>

                    <form class="layui-form" id="hostForm" action="pick" method="post">
                        <div class="layui-form-item">
                            <label class="layui-form-label" for="email">邮箱</label>
                            <div class="layui-input-block">
                                <input type="text" id="email" name="email" required lay-verify="required" placeholder="邮箱" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label" for="captcha">验证码</label>
                            <div class="layui-input-inline">
                                <input type="text" id="captcha" name="captcha" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <img id="captchaImage" src="{:captcha_src()}" alt="验证码" style="cursor: pointer;" title="点击刷新">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn layui-btn-normal layui-btn-fluid" lay-submit lay-filter="formSubmit">提交</button>
                            </div>
                        </div>
                        <input type="hidden" id="csrf_token" name="csrf_token" value="{$uuid}">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

    <script src="static/js/layui.js"></script>
    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;

            document.getElementById("captchaImage").onclick = function() {
                this.src = '{:captcha_src()}';
            };

            form.on('submit(formSubmit)', function(data) {
                var formData = new URLSearchParams();
                formData.append("email", data.field.email);
                formData.append("captcha", data.field.captcha);
                formData.append("csrf_token", document.getElementById("csrf_token").value);

                var loadingIndex = layer.load(1, {
                    shade: [0.3, '#000'] 
                });

                fetch("index/index/pick", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    body: formData
                })
                .then(response => response.json())
                .then(result => {
                    layer.close(loadingIndex); 
                    if (result.code === 200) {
                        layer.open({
                            content: '领取成功！<br>卡密已发送到你的邮箱',
                            icon: 1,
                            title: "提示",
                            btn: ['确定'],
                            shadeClose: false,
                            yes: function(index) {
                                layer.close(index);
                                location.reload();
                            }
                        });
                    } else {
                        layer.open({
                            content: result.msg || "请求失败，请重试",
                            icon: 2,
                            title: "错误",
                            btn: ['确定'],
                            shadeClose: false,
                            yes: function(index) {
                                layer.close(index);
                                location.reload();
                            }
                        });
                    }
                })
                .catch(error => {
                    layer.close(loadingIndex); 
                    layer.open({
                        content: "网络错误，请稍后再试",
                        icon: 2,
                        title: "错误",
                        btn: ['确定'],
                        shadeClose: false,
                        yes: function(index) {
                            layer.close(index);
                            location.reload();
                        }
                    });
                });
                return false;
            });
        });
    </script>
</body>
</html>
